package cn.hzking.gjmall.app;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.tencent.smtt.sdk.QbSdk;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.constant.Constants;
import cn.hzking.gjmall.netstate.NetChangeObserver;
import cn.hzking.gjmall.netstate.NetWorkUtil;
import cn.hzking.gjmall.netstate.NetworkStateReceiver;
import cn.hzking.gjmall.utils.Logger;
import cn.hzking.gjmall.utils.ThreadManager;
import cn.hzking.gjmall.utils.UIUtils;

/**
 * Created by Administrator on 2018/1/24.
 */

public class GjApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Gjmall.init(this)
                .withOssRealseHost("http://gjmall.oss-cn-hangzhou.aliyuncs.com/")//阿里云正式域名
                .withRealseAppHost("http://admin.hzking.cn/")//正式服务器域名
                .withDeBugAppHost("http://10.220.110.135:8081/")//测试服务器域名
                //.withDeBugAppHost("http://10.220.115.19:8081/")
                .withOssDeBugHost("http://gjmall-dev.oss-cn-hangzhou.aliyuncs.com/")//阿里云测试域名
                .withWeChatAppId("wxb4c2548c578f2050")//appId
                .withDeBugWeChatAppId("wxb4c2548c578f2050")
                .withJavascriptInterface("gjmall")
                .configure();
        ThreadManager.execute(new Runnable() {
            @Override
            public void run() {
                initNetState();
            }
        });
        ThreadManager.execute(new Runnable() {
            @Override
            public void run() {
                //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
                QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

                    @Override
                    public void onViewInitFinished(boolean arg0) {
                        // TODO Auto-generated method stub
                        //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                        Logger.e("app", " onViewInitFinished is " + arg0);
                    }

                    @Override
                    public void onCoreInitFinished() {
                        // TODO Auto-generated method stub
                    }
                };
                //x5内核初始化接口
                QbSdk.initX5Environment(getApplicationContext(),  cb);
            }
        });


    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        /*
        如果App是多进程架构的话，Application会执行多次，这个优化过程无需执行多次；
        而在SDK版本5.0及以上，默认使用ART虚拟机，与Dalvik的区别在于安装时已经将全部的Class.dex转换为了oat文件，
        优化过程在安装时已经完成；因此无需执行。
         */
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            MultiDex.install(GjApp.this);//方法过多分区

        }
    }

    private void initNetState() {
        NetworkStateReceiver.registerNetworkStateReceiver(this);
        NetworkStateReceiver.registerObserver(observer);
    }

    public void unegisterNetworkStateReceiver() {
        NetworkStateReceiver.removeRegisterObserver(observer);
        NetworkStateReceiver.unRegisterNetworkStateReceiver(this);
    }

    private NetChangeObserver observer = new NetChangeObserver() {
        @Override
        public void onConnect(NetWorkUtil.NetType type) {
            super.onConnect(type);
           Constants.isNetConnect = true;
            //  EventBus.getDefault().post(new EventBean("load"));
        }

        @Override
        public void onDisConnect() {
            super.onDisConnect();
            Constants.isNetConnect = false;
        }
    };
}
